package com.bitdf.txing.users.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.users.dao.UserRolePermRelateDao;
import com.bitdf.txing.users.entity.UserPermissionEntity;
import com.bitdf.txing.users.entity.UserRolePermRelateEntity;
import com.bitdf.txing.users.service.UserPermissionService;
import com.bitdf.txing.users.service.UserRolePermRelateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service("userRolePermRelateService")
public class UserRolePermRelateServiceImpl extends ServiceImpl<UserRolePermRelateDao, UserRolePermRelateEntity> implements UserRolePermRelateService {

    @Autowired
    UserPermissionService userPermissionService;

    /**
     * 查询某角色拥有的所有权限
     * @param roleId
     * @return
     */
    @Override
    public List<UserPermissionEntity> getPermsByRoleId(String roleId) {
        // 查出该角色的所有权限关联记录
        QueryWrapper<UserRolePermRelateEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserRolePermRelateEntity::getRoleId, Long.parseLong(roleId));
        List<UserRolePermRelateEntity> list = this.list(wrapper);
        // 查出这些关联的权限的具体信息， 并封装成list
        List<UserPermissionEntity> collect = list.stream().map((item) -> {
            UserPermissionEntity entity = userPermissionService.getById(item.getPermId());
            return entity;
        }).collect(Collectors.toList());
        return collect;
    }
}